<!doctype html>
<html lang="zh-CN" th:replace="admin-layout :: layout(~{::title}, ~{::link}, ~{::script}, ~{::body}, 'paper-list')"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>试卷试题</title>
    <link rel="stylesheet" th:href="@{/common/zTree/css/demo.css}" type="text/css">
    <link rel="stylesheet" th:href="@{/common/zTree/css/zTreeStyle/zTreeStyle.css}" type="text/css">
    <script type="text/javascript" th:src="@{/common/zTree/js/jquery.ztree.core.min.js}"></script>
    <script type="text/javascript" th:src="@{/common/zTree/js/jquery.ztree.excheck.min.js}"></script>
    <script>
        var $tableA = $("#tableA");
        var $tableB = $("#tableB");
        var $tableC = $("#tableC");
        var $tableD = $("#tableD");
        var $selectQstBtnA = $("#selectQstBtnA");
        var $deleteQstBtnA = $("#deleteQstBtnA");
        var $selectQstBtnB = $("#selectQstBtnB");
        var $deleteQstBtnB = $("#deleteQstBtnB");
        var $selectQstBtnC = $("#selectQstBtnC");
        var $deleteQstBtnC = $("#deleteQstBtnC");
        var $selectQstBtnD = $("#selectQstBtnD");
        var $deleteQstBtnD = $("#deleteQstBtnD");
        var $setQstDefScoreBtnA = $("#setQstDefScoreBtnA");
        var $setQstDefScoreBtnB = $("#setQstDefScoreBtnB");
        var $setQstDefScoreBtnC = $("#setQstDefScoreBtnC");
        var $setQstDefScoreBtnD = $("#setQstDefScoreBtnD");
        var $setQstDefNumBtnA = $("#setQstDefNumBtnA");
        var $setQstDefNumBtnB = $("#setQstDefNumBtnB");
        var $setQstDefNumBtnC = $("#setQstDefNumBtnC");
        var $setQstDefNumBtnD = $("#setQstDefNumBtnD");
        var selectionsA = [];
        var selectionsB = [];
        var selectionsC = [];
        var selectionsD = [];

        $deleteQstBtnA.click(function () {
            confirmAlert(function () {
                ajaxJson("/admin/paper/rule/ajax/delete", {"ids": selectionsA.toString(),"paperId":$("#paperId").val()}, function (res) {
                    if (res.status) {
                        $deleteQstBtnA.prop('disabled', true);
                        if (res.data.typeA==0){
                            $setQstDefScoreBtnA.prop('disabled', true);
                            $setQstDefNumBtnA.prop('disabled', true);
                        }
                        refreshTableA();
                    }
                })
            })
        })

        $deleteQstBtnB.click(function () {
            confirmAlert(function () {
                ajaxJson("/admin/paper/rule/ajax/delete", {"ids": selectionsB.toString(),"paperId":$("#paperId").val()}, function (res) {
                    if (res.status) {
                        $deleteQstBtnB.prop('disabled', true);
                        if (res.data.typeB==0){
                            $setQstDefScoreBtnB.prop('disabled', true);
                            $setQstDefNumBtnB.prop('disabled', true);
                        }
                        refreshTableB();
                    }
                })
            })
        })

        $deleteQstBtnC.click(function () {
            confirmAlert(function () {
                ajaxJson("/admin/paper/rule/ajax/delete", {"ids": selectionsC.toString(),"paperId":$("#paperId").val()}, function (res) {
                    if (res.status) {
                        $deleteQstBtnC.prop('disabled', true);
                        if (res.data.typeC==0){
                            $setQstDefScoreBtnC.prop('disabled', true);
                            $setQstDefNumBtnC.prop('disabled', true);
                        }
                        refreshTableC();
                    }
                })
            })
        })

        $deleteQstBtnD.click(function () {
            confirmAlert(function () {
                ajaxJson("/admin/paper/rule/ajax/delete", {"ids": selectionsD.toString(),"paperId":$("#paperId").val()}, function (res) {
                    if (res.status) {
                        $deleteQstBtnD.prop('disabled', true);
                        if (res.data.typeD==0){
                            $setQstDefScoreBtnD.prop('disabled', true);
                            $setQstDefNumBtnD.prop('disabled', true);
                        }
                        refreshTableD();
                    }
                })
            })
        })

        var columns = [{
            checkbox: true
        }, {
            field: 'classifyName',
            title: '分类名称',
        }, {
            field: 'qstNum',
            title: '抽题数量',
        }, {
            field: 'qstScore',
            title: '每题分数',
        }]

        $(document).ready(function () {
            initTable($tableA, columns, function () {
                $deleteQstBtnA.prop('disabled', !$tableA.bootstrapTable('getSelections').length);

                selectionsA = getIdSelectionsA()
            })
            $("#qstType").val("B");
            initTable($tableB, columns, function () {
                console.log($tableB.bootstrapTable('getSelections').length)
                $deleteQstBtnB.prop('disabled', !$tableB.bootstrapTable('getSelections').length);

                selectionsB = getIdSelectionsB()
            })
            $("#qstType").val("C");
            initTable($tableC, columns, function () {
                console.log($tableC.bootstrapTable('getSelections').length)
                $deleteQstBtnC.prop('disabled', !$tableC.bootstrapTable('getSelections').length);

                selectionsC = getIdSelectionsC()
            })
            $("#qstType").val("D");
            initTable($tableD, columns, function () {
                console.log($tableD.bootstrapTable('getSelections').length)
                $deleteQstBtnD.prop('disabled', !$tableD.bootstrapTable('getSelections').length);

                selectionsD = getIdSelectionsD()
            })
            $("#qstType").val("A");
        });

        function getIdSelectionsA() {
            return $.map($tableA.bootstrapTable('getSelections'), function (row) {
                return row.id
            })
        }

        function getIdSelectionsB() {
            return $.map($tableB.bootstrapTable('getSelections'), function (row) {
                return row.id
            })
        }

        function getIdSelectionsC() {
            return $.map($tableC.bootstrapTable('getSelections'), function (row) {
                return row.id
            })
        }

        function getIdSelectionsD() {
            return $.map($tableD.bootstrapTable('getSelections'), function (row) {
                return row.id
            })
        }

        $selectQstBtnA.click(function(){
            ajaxJson("/admin/paper/rule/classifyIds",{"paperId":$("#paperId").val(),"qstType":$("#qstType").val()},function(res){
                openWindowBtn("选择分类","/admin/select/classify",{"noDataIds":res.data},function(){
                    if (childSelections.length>0) {
                        ajaxJson("/admin/paper/rule/ajax/add",{"paperId":$("#paperId").val(),"qstType":$("#qstType").val(),"classifyIds":childSelections.toString()},function(res){
                            if(res.status){
                                $("#noDataIds").val(res.data.classifyIds);
                                if (res.data.typeA>0){
                                    $setQstDefScoreBtnA.prop('disabled', false);
                                    $setQstDefNumBtnA.prop('disabled', false);
                                }
                                refreshTableA();
                            }else{
                                msgTips("保存失败");
                            }
                        })
                    }
                })
            })
        })

        $selectQstBtnB.click(function(){
            ajaxJson("/admin/paper/rule/classifyIds",{"paperId":$("#paperId").val(),"qstType":$("#qstType").val()},function(res){
                openWindowBtn("选择分类","/admin/select/classify",{"noDataIds":res.data},function(){
                    if (childSelections.length>0) {
                        ajaxJson("/admin/paper/rule/ajax/add",{"paperId":$("#paperId").val(),"qstType":$("#qstType").val(),"classifyIds":childSelections.toString()},function(res){
                            if(res.status){
                                $("#noDataIds").val(res.data.classifyIds);
                                if (res.data.typeB>0){
                                    $setQstDefScoreBtnB.prop('disabled', false);
                                    $setQstDefNumBtnB.prop('disabled', false);
                                }
                                refreshTableB();
                            }else{
                                msgTips("保存失败");
                            }
                        })
                    }
                })
            })
        })

        $selectQstBtnC.click(function(){
            ajaxJson("/admin/paper/rule/classifyIds",{"paperId":$("#paperId").val(),"qstType":$("#qstType").val()},function(res){
                openWindowBtn("选择分类","/admin/select/classify",{"noDataIds":res.data},function(){
                    if (childSelections.length>0) {
                        ajaxJson("/admin/paper/rule/ajax/add",{"paperId":$("#paperId").val(),"qstType":$("#qstType").val(),"classifyIds":childSelections.toString()},function(res){
                            if(res.status){
                                $("#noDataIds").val(res.data.classifyIds);
                                if (res.data.typeC>0){
                                    $setQstDefScoreBtnC.prop('disabled', false);
                                    $setQstDefNumBtnC.prop('disabled', false);
                                }
                                refreshTableC();
                            }else{
                                msgTips("保存失败");
                            }
                        })
                    }
                })
            })
        })

        $selectQstBtnD.click(function(){
            ajaxJson("/admin/paper/rule/classifyIds",{"paperId":$("#paperId").val(),"qstType":$("#qstType").val()},function(res){
                openWindowBtn("选择分类","/admin/select/classify",{"noDataIds":res.data},function(){
                    if (childSelections.length>0) {
                        ajaxJson("/admin/paper/rule/ajax/add",{"paperId":$("#paperId").val(),"qstType":$("#qstType").val(),"classifyIds":childSelections.toString()},function(res){
                            if(res.status){
                                $("#noDataIds").val(res.data.classifyIds);
                                if (res.data.typeD>0){
                                    $setQstDefScoreBtnD.prop('disabled', false);
                                    $setQstDefNumBtnD.prop('disabled', false);
                                }
                                refreshTableD();
                            }else{
                                msgTips("保存失败");
                            }
                        })
                    }
                })
            })
        })

        //标签页切换
        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            var type = $(this).data("type")
            $("#qstType").val(type);
        })

        function refreshTableA() {
            $tableA.bootstrapTable('refresh')
        }

        function refreshTableB() {
            $tableB.bootstrapTable('refresh')
        }

        function refreshTableC() {
            $tableC.bootstrapTable('refresh')
        }

        function refreshTableD() {
            $tableD.bootstrapTable('refresh')
        }

        $setQstDefScoreBtnA.click(function () {
            debugger
            var title = "设置所有单选题分数";
            var id = "";
            var v = "";
            if (selectionsA.length > 0) {
                title = "设置选中试题分数";
                id = selectionsA[0];
                v = v+getQstScoreSelectionsA()[0];
            }
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/paper/rule/ajax/updateQstScore", {"id": id, "qstType": $("#qstType").val(), "paperId": $("#paperId").val(), "qstScore": value}, function (res) {
                        if (res.status) {
                            refreshTableA();
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        $setQstDefScoreBtnB.click(function () {
            var title = "设置所有多选题分数";
            var id = "";
            var v = "";
            if (selectionsB.length > 0) {
                title = "设置选中试题分数";
                id = selectionsB[0];
                v = v+getQstScoreSelectionsB()[0];
            }
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/paper/rule/ajax/updateQstScore", {"id": id, "qstType": $("#qstType").val(), "paperId": $("#paperId").val(), "qstScore": value}, function (res) {
                        if (res.status) {
                            refreshTableB();
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        $setQstDefScoreBtnC.click(function () {
            var title = "设置所有判断题分数";
            var id = "";
            var v = "";
            if (selectionsC.length > 0) {
                title = "设置选中试题分数";
                id = selectionsC[0];
                v = v+getQstScoreSelectionsC()[0];
            }
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/paper/rule/ajax/updateQstScore", {"id": id, "qstType": $("#qstType").val(), "paperId": $("#paperId").val(), "qstScore": value}, function (res) {
                        if (res.status) {
                            refreshTableC();
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        $setQstDefScoreBtnD.click(function () {
            var title = "设置所有主观题分数";
            var id = "";
            var v = "";
            if (selectionsD.length > 0) {
                title = "设置选中试题分数";
                id = selectionsD[0];
                v = v+getQstScoreSelectionsD()[0];
            }
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/paper/rule/ajax/updateQstScore", {"id": id, "qstType": $("#qstType").val(), "paperId": $("#paperId").val(), "qstScore": value}, function (res) {
                        if (res.status) {
                            refreshTableD();
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        /////////
        $setQstDefNumBtnA.click(function () {
            var title = "设置所有单选题分类抽题数量";
            var id = "";
            var v = "";
            if (selectionsA.length > 0) {
                title = "设置选中分类抽题数量";
                id = selectionsA[0];
                v = v+getQstNumSelectionsA()[0];
            }
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/paper/rule/ajax/updateQstNum", {"id": id, "qstType": $("#qstType").val(), "paperId": $("#paperId").val(), "qstNum": value}, function (res) {
                        if (res.status) {
                            refreshTableA();
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        $setQstDefNumBtnB.click(function () {
            var title = "设置所有多选题分类抽题数量";
            var id = "";
            var v = "";
            if (selectionsB.length > 0) {
                title = "设置选中分类抽题数量";
                id = selectionsB[0];
                v = v+getQstNumSelectionsB()[0];
            }
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/paper/rule/ajax/updateQstNum", {"id": id, "qstType": $("#qstType").val(), "paperId": $("#paperId").val(), "qstNum": value}, function (res) {
                        if (res.status) {
                            refreshTableB();
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        $setQstDefNumBtnC.click(function () {
            var title = "设置所有判断题分类抽题数量";
            var id = "";
            var v = "";
            if (selectionsC.length > 0) {
                title = "设置选中分类抽题数量";
                id = selectionsC[0];
                v = v+getQstNumSelectionsC()[0];
            }
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/paper/rule/ajax/updateQstNum", {"id": id, "qstType": $("#qstType").val(), "paperId": $("#paperId").val(), "qstNum": value}, function (res) {
                        if (res.status) {
                            refreshTableC();
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        $setQstDefNumBtnD.click(function () {
            var title = "设置所有主观题分类抽题数量";
            var id = "";
            var v = "";
            if (selectionsD.length > 0) {
                title = "设置选中分类抽题数量";
                id = selectionsD[0];
                v = v+getQstNumSelectionsD()[0];
            }
            layer.prompt({
                value: v,
                title: title,
            }, function (value, index, elem) {
                if (isNumber(value)) {
                    ajaxJson("/admin/paper/rule/ajax/updateQstNum", {"id": id, "qstType": $("#qstType").val(), "paperId": $("#paperId").val(), "qstNum": value}, function (res) {
                        if (res.status) {
                            refreshTableD();
                            layer.close(index);
                        }
                    })
                } else {
                    msgTips("请输入整数")
                }
            });
        })

        function getQstScoreSelectionsA() {
            return $.map($tableA.bootstrapTable('getSelections'), function (row) {
                return row.qstScore
            })
        }

        function getQstScoreSelectionsB() {
            return $.map($tableB.bootstrapTable('getSelections'), function (row) {
                return row.qstScore
            })
        }

        function getQstScoreSelectionsC() {
            return $.map($tableC.bootstrapTable('getSelections'), function (row) {
                return row.qstScore
            })
        }

        function getQstScoreSelectionsD() {
            return $.map($tableD.bootstrapTable('getSelections'), function (row) {
                return row.qstScore
            })
        }

        function getQstNumSelectionsA() {
            return $.map($tableA.bootstrapTable('getSelections'), function (row) {
                return row.qstNum
            })
        }

        function getQstNumSelectionsB() {
            return $.map($tableB.bootstrapTable('getSelections'), function (row) {
                return row.qstNum
            })
        }

        function getQstNumSelectionsC() {
            return $.map($tableC.bootstrapTable('getSelections'), function (row) {
                return row.qstNum
            })
        }

        function getQstNumSelectionsD() {
            return $.map($tableD.bootstrapTable('getSelections'), function (row) {
                return row.qstNum
            })
        }
    </script>
</head>
<body>
<!--<ol class="breadcrumb">
    <li><a th:href="@{/admin/paper/list}">试卷列表</a></li>
    <li class="active">出题规则</li>
</ol>-->
<form id="toolbar">
    <input type="hidden" name="paperId" id="paperId" th:value="${paperId}">
    <input type="hidden" name="qstType" id="qstType" value="A">
</form>
<div>

    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#radio" aria-controls="radio" data-type="A" role="tab"
                                                  data-toggle="tab">单选题</a></li>
        <li role="presentation"><a href="#checkbox" aria-controls="checkbox" data-type="B" role="tab"
                                   data-toggle="tab">多选题</a></li>
        <li role="presentation"><a href="#judge" aria-controls="judge" data-type="C" role="tab"
                                   data-toggle="tab">判断题</a></li>
        <li role="presentation"><a href="#subjective" aria-controls="subjective" data-type="D" role="tab"
                                   data-toggle="tab">主观题</a>
        </li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active" id="radio">
            <div class="fyl-mb20">
                <button type="button" class="btn btn-primary" id="selectQstBtnA">选择抽题分类</button>
                <button type="button" class="btn btn-primary" id="setQstDefNumBtnA" th:disabled="${typeCount.typeA}==0">设置抽题数量</button>
                <button type="button" class="btn btn-primary" id="setQstDefScoreBtnA" th:disabled="${typeCount.typeA}==0">设置试题分数</button>
                <button type="button" class="btn btn-danger" id="deleteQstBtnA" disabled>删除抽题分类</button>
            </div>
            <!--单选题-->
            <table id="tableA" data-url="/admin/paper/rule/ajax/list"></table>
        </div>
        <div role="tabpanel" class="tab-pane" id="checkbox">
            <div class="fyl-mb20">
                <button type="button" class="btn btn-primary" id="selectQstBtnB">选择抽题分类</button>
                <button type="button" class="btn btn-primary" id="setQstDefNumBtnB" th:disabled="${typeCount.typeB}==0">设置抽题数量</button>
                <button type="button" class="btn btn-primary" id="setQstDefScoreBtnB" th:disabled="${typeCount.typeB}==0">设置试题分数</button>
                <button type="button" class="btn btn-danger" id="deleteQstBtnB" disabled>删除抽题分类</button>
            </div>
            <!--多选题-->
            <table id="tableB" data-url="/admin/paper/rule/ajax/list"></table>
        </div>
        <div role="tabpanel" class="tab-pane" id="judge">
            <div class="fyl-mb20">
                <button type="button" class="btn btn-primary" id="selectQstBtnC">选择抽题分类</button>
                <button type="button" class="btn btn-primary" id="setQstDefNumBtnC" th:disabled="${typeCount.typeC}==0">设置抽题数量</button>
                <button type="button" class="btn btn-primary" id="setQstDefScoreBtnC" th:disabled="${typeCount.typeC}==0">设置试题分数</button>
                <button type="button" class="btn btn-danger" id="deleteQstBtnC" disabled>删除抽题分类</button>
            </div>
            <!--判断题-->
            <table id="tableC" data-url="/admin/paper/rule/ajax/list"></table>
        </div>
        <div role="tabpanel" class="tab-pane" id="subjective">
            <div class="fyl-mb20">
                <button type="button" class="btn btn-primary" id="selectQstBtnD">选择抽题分类</button>
                <button type="button" class="btn btn-primary" id="setQstDefNumBtnD" th:disabled="${typeCount.typeD}==0">设置抽题数量</button>
                <button type="button" class="btn btn-primary" id="setQstDefScoreBtnD" th:disabled="${typeCount.typeD}==0">设置试题分数</button>
                <button type="button" class="btn btn-danger" id="deleteQstBtnD" disabled>删除抽题分类</button>
            </div>
            <!--主观题-->
            <table id="tableD" data-url="/admin/paper/rule/ajax/list"></table>
        </div>
    </div>

</div>
</body>
</html>